import { fileURLToPath, URL } from 'node:url'

import { defineConfig,loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { VantResolver } from '@vant/auto-import-resolver';

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  // Load env file based on `mode` in the current working directory.
  // Set the third parameter to '' to load all env regardless of the `VITE_` prefix.
  const { VITE_MAP_URL,VITE_CITY_URL,VITE_AREA_URL } = loadEnv(mode, process.cwd(), '')
  return {
    plugins: [
      vue(),
      AutoImport({
        resolvers: [VantResolver()],
      }),
      Components({
        resolvers: [VantResolver()],
      }),
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    server:{
      proxy:{
        '/map':{
          target:VITE_MAP_URL,
          changeOrigin:true,
          rewrite:path=>path.replace(/^\/map/,'')
        },
        '/citys':{
          target:VITE_CITY_URL,
          changeOrigin:true,
          rewrite:path=>path.replace(/^\/citys/,'')
        },
        '/area':{
          target:VITE_AREA_URL,
          changeOrigin:true,
          rewrite:path=>path.replace(/^\/area/,'')
        }
      }
    }
  }
})